Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
             {name:'employe_id',type:'string'},
             {name:'employe__name',type:'string'},
             {name:'state',type:'string'},
             {name:'month',type:'string'},
             {name:'self_no',type:'string'},
             {name:'department_no',type:'string'},
             {name:'department_man_id',type:'string'},
             {name:'department_man__name',type:'string'},
             
             
             {name:'headquarter_no',type:'string'},
             {name:'headquarter_man_id',type:'string'},
             {name:'headquarter_man__name',type:'string'},             
             
             {name:'finally_no',type:'string'},
             {name:'finally_man_id',type:'string'},
             {name:'finally_man__name',type:'string'},
             {name:'remark',type:'string'},
             {name:'content',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/employecontrol/employeperformance_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: false
    });
    
    //校区combox
    var companyComboboxForm= Ext.create('Ext.form.field.ComboBox', {
        id:'company_id',
	   	name:'company_id',
       	fieldLabel: '校区', 
       	allowBlank : false,
       	editable:false,
       	emptyText: '请选择',
       	store: new Ext.data.Store({ 
           	singleton : true, 
	   	    proxy: {
	   	        type: 'ajax',
	   	        url: '/resourcecontrol/company_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'cn_name'], 
           autoLoad:true 
       	}), 
       	loadingText:'正在加载数据，请稍侯……', 
       	triggerAction:'all', 
       	valueField:'id', 
       	displayField:'cn_name'
    });
    
    //员工combox
    var employeComboBox= Ext.create('Ext.form.field.ComboBox', { 
       id : 'employe_combobox_id',
	   name:'employe_id',
       fieldLabel: '员工', 
       typeAhead:true,
       minChars:1,
       editable:true,
       allowBlank : false,
       blankText : '请选择员工	.',
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/employecontrol/employe_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name',
       listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
      //状态combox
    var contractStateComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'state_combobox_id',
    	name:'state',
    	emptyText: '请选择状态',
    	labelWidth: 40,
    	width : 160,
    	fieldLabel: '状态', 
    	editable:false,
    	store: new Ext.data.Store({ 
			fields:['id','value'], 
			data:[
		     //  状态 0预制，1有效，11部门待审核,12校区待审核,13总部待审核,  23总部驳回,22校区驳回，21主管驳回
			    {'id':'' ,'value':'全部'}, 
				{'id':'0','value':'预制'},
				{'id':'1','value':'有效'},
				{'id':'11','value':'部门待审核'},
				{'id':'12','value':'校区待审核'},
				{'id':'13','value':'总部待审核'},
				{'id':'23','value':'总部驳回'},
				{'id':'22','value':'校区驳回'},
				{'id':'21','value':'主管驳回'}
			],
	    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'value'
    }); 
    
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false},
			{text: "员工ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
			{text: "员工", width: 80, dataIndex: 'employe__name', sortable: false},
			{text: "状态", width: 80, dataIndex: 'state', sortable: false, renderer:function(v){
				// 状态 0预制，1有效，11部门待审核,12校区审核,13总部待审核,  23总部驳回,22校区驳回，21主管驳回
            	if (v==0) 
                	return '预制';
    			else if(v==1)
        			return '有效';
    			else if(v==11)
        			return '部门待审核';
    			else if(v==12)
        			return '校区待审核';
    			else if(v==13)
        			return '总部待审核';
    			else if(v==23)
        			return '总部驳回';
    			else if(v==22)
        			return '校区驳回';
    			else if(v==21)
        			return '主管驳回' ;       			
            	}
            },
			{text: "个人自评分", width: 80, dataIndex: 'self_no', sortable: false},
			{text: "部门考核分", width: 80, dataIndex: 'department_no', sortable: false},
			{text: "部门考核人ID", width: 80, dataIndex: 'department_man_id', sortable: false,hidden : true},
			{text: "部门考核人", width: 80, dataIndex: 'department_man__name', sortable: false},
			
			{text: "校区考核分", width: 80, dataIndex: 'headquarter_no', sortable: false},
			{text: "校区考核人ID", width: 80, dataIndex: 'headquarter_man_id', sortable: false,hidden : true},
			{text: "校区考核人", width: 80, dataIndex: 'headquarter_man__name', sortable: false},			
						
			
			{text: "最终核定分", width: 80, dataIndex: 'finally_no', sortable: false},
			{text: "最终核定人ID", width: 80, dataIndex: 'finally_man_id', sortable: false,hidden : true},
			{text: "最终核定人", width: 80, dataIndex: 'finally_man__name', sortable: false}
//			{text: "备注", width: 80, dataIndex: 'remark', sortable: false}
			
        ],
        //title: '考勤管理',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
//                	form.getForm().reset();
//        			var selected = grid.getSelectionModel().getSelection();
//					form.loadRecord(selected[0]);
//        			win.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype: 'combobox',
        		id:'company_combox_id',
            	name:'company',
            	labelWidth: 40,
            	width: 140,
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({ 
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true 
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name'        		
        	},*/contractStateComboBox,{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
//	        		if(Ext.getCmp('company_combox_id').getValue()!=null){
//	        			params = { company_id:Ext.getCmp('company_combox_id').getValue() };
//	        		    Ext.apply(store.proxy.extraParams, params);
//	        		    store.currentPage=1;
//	        		    store.load();
//	    			}else{
//	    				Ext.Msg.alert('系统提示', '请选择校区!');
//	    			}
        			//params = { company_id:Ext.getCmp('company_combox_id').getValue() };
        			params = {state: Ext.getCmp('state_combobox_id').getValue()};
        		    Ext.apply(store.proxy.extraParams, params);
        		    store.currentPage=1;
        		    store.load();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				var st = 0 ; // 状态
	        			Ext.each(selected, function (item) {
	        				st = item.data.state ;
	        			});
	        			
	        			if(st==0 || st==1|| st==3|| st==6){
	        				Ext.Msg.alert('系统提示', '您无权对当前记录进行操作!');
	        				return ;
	        			}
	    				
	    				form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的记录!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'提交审核',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认提交审核吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	        					var st = 0 ; // 状态
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id;
	                				st = item.data.state ;
	                			});
	                			
	                			if(st==0 || st==1|| st==3|| st==6){
	                				Ext.Msg.alert('系统提示', '您无权对当前记录进行操作!');
	                				return ;
	                			}
	                			
	                			Ext.Ajax.request({
	                			    url:'/employecontrol/employeperformance_update_state1/',
	                			    method : 'post',
	                			    params:{
	                			    	id:ids,
	                			    	state:'3'// 总部待审核
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	store.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要提交审核的记录!');
	    			}
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		width: 850,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
				xtype:'hiddenfield',
				fieldLabel:'id',
				name:'id'
			},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '部门考核分',
				name : 'department_no',
				allowBlank : false,
				value:'85',
				blankText : '请输入部门考核分		.'
			}]
		},{
            xtype: 'htmleditor',
            height:380,
            fieldLabel: '自评内容',
            name: 'content',
            flex:1
        }]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/employecontrol/employeperformance_update_dep_no/',
						success : function(form, action) {
							//Ext.Msg.alert('系统提示',  action.result.message);
							//form.getForm().reset();
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	// 适应屏幕
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'performance-hd-grid',
		items:[grid]
	});
	
});